package com.yisu.expressway.service_district;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.umeng.socialize.utils.Log;
import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.adapter.GasListAdapter;
import com.yisu.expressway.adapter.ScenicSpotListAdapter;
import com.yisu.expressway.adapter.ServiceListAdapter;
import com.yisu.expressway.adapter.ShopListAdapter;
import com.yisu.expressway.model.ServiceDistrictObj;
import com.yisu.expressway.model.ServiceDistrictItem;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.ui.SlideShowView;
import com.yisu.expressway.ui.recyclerview.DividerListItemDecoration;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;


public class DistrictDetailActivity extends BaseActivity {

    @Bind(R.id.tv_title)
    protected TextView mTv_title;
    @Bind(R.id.tv_name)
    protected TextView mTv_select_district_name;

    @Bind(R.id.content_root)
    protected NestedScrollView mContentRoot;
    @Bind(R.id.tv_service_detail_title)
    protected TextView mServiceDetailTitle;
    @Bind(R.id.tv_good_shop)
    protected TextView mTv_goodShopTitle;
    @Bind(R.id.tv_nearby_scenic_title)
    protected TextView mTv_nearby_view_title;

    @Bind(R.id.slide_banner)
    protected SlideShowView mBannerSiler;
    @Bind(R.id.rv_gas_list)
    protected RecyclerView mRv_gas_list;
    @Bind(R.id.rv_service_sorts)
    protected RecyclerView mRv_service_list;
    @Bind(R.id.rv_shop_list)
    protected RecyclerView mRv_shop_list;
    @Bind(R.id.rv_scenic_spot_list)
    protected RecyclerView mRv_scenic_spot_list;

    private View parentView;


    private ServiceListAdapter mServiceAdapter;
    private GasListAdapter mGasAdapter;
    private ShopListAdapter mShopAdapter;
    private ScenicSpotListAdapter mScenicSpotAdapter;

    private static final String SELECT_SERVICE_DISTRICT = "select_service_district";
    public ServiceDistrictItem mSelectDistrict;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district_detail);
        init(getIntent());
        initRecyclerView();
        getSelectServiceDistrictDetail(mSelectDistrict);
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        init(intent);
        getSelectServiceDistrictDetail(mSelectDistrict);
    }

    public static void launch(Activity callActivity, ServiceDistrictItem item) {
        Intent mIntent = new Intent(callActivity, DistrictDetailActivity.class);
        mIntent.putExtra(SELECT_SERVICE_DISTRICT, item);
        callActivity.startActivity(mIntent);
        callActivity.finish();
    }

    public static void launch(Activity callActivity, ServiceDistrictItem item,boolean isFinishLast) {
        Intent mIntent = new Intent(callActivity, DistrictDetailActivity.class);
        mIntent.putExtra(SELECT_SERVICE_DISTRICT, item);
        callActivity.startActivity(mIntent);
        if(isFinishLast) {
            callActivity.finish();
        }
    }

    private void init(Intent intent) {
        mSelectDistrict = (ServiceDistrictItem) intent.getSerializableExtra(SELECT_SERVICE_DISTRICT);
    }

    public ServiceDistrictItem getSelectDistrict() {
        return mSelectDistrict;
    }

    @OnClick(R.id.iv_refresh_location)
    public void onRefreshServiceList(){
        ServiceDistrictListActivity.launch(this,true);
    }


    @OnClick(R.id.iv_btn_back)
    public void goBack(){
        finish();
    }

    /**
     * 获取选中服务区详情
     */
    private void getSelectServiceDistrictDetail(ServiceDistrictItem item){
        if(item == null){
            return;
        }

        showLoading();

        Map<String, Object> params = new HashMap();
        params.put("serviceId", item.id);
        ApiClientHelper.postApi(ApiSetting.getServiceDistrictDetail(), new TypeToken<ServiceDistrictObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ServiceDistrictObj>>() {
            @Override
            public void onResponse(ApiResult<ServiceDistrictObj> result) {

                dismissLoading();

                if (!result.success) {
                    ToastUtil.displayLongToastMsg(DistrictDetailActivity.this, result.getMsg());
                    return;
                }

                initServiceDistrictView(result.getResult());


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismissLoading();
                ToastUtil.displayShortToastMsg(DistrictDetailActivity.this, volleyError.getMessage());
            }
        }, this);
    }

    /**
     * 初始化recyclver View divider
     */
    private void initRecyclerView(){

        mRv_gas_list.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_service_list.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_service_list.addItemDecoration(new DividerListItemDecoration(this,LinearLayoutManager.HORIZONTAL,4,R.color.transparent));

        mRv_shop_list.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_shop_list.addItemDecoration(new DividerListItemDecoration(this,LinearLayoutManager.HORIZONTAL,4,R.color.transparent));

        mRv_scenic_spot_list.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_scenic_spot_list.addItemDecoration(new DividerListItemDecoration(this,LinearLayoutManager.HORIZONTAL,4,R.color.transparent));
    }

    /**
     * 初始化选中的服务区UI
     * @param serviceObj 服务区详情model
     */
    private void initServiceDistrictView(ServiceDistrictObj serviceObj){


        ServiceDistrictObj.ServiceDistrictDetail mDetail = serviceObj.serviceAreaDetail;

        mContentRoot.setVisibility(View.VISIBLE);
        mContentRoot.scrollTo(0,0);


        mTv_title.setText(mDetail.serviceName);
        mTv_select_district_name.setText(mDetail.expressWayName);
        mBannerSiler.removeAllViews();
        mBannerSiler.initData(this,serviceObj.bannerList);

        if(mGasAdapter == null){
            mGasAdapter = new GasListAdapter(this);
        }
        mRv_gas_list.setAdapter(mGasAdapter);
        mGasAdapter.setData(mDetail.gasList);

        if(mServiceAdapter == null){
            mServiceAdapter = new ServiceListAdapter(this);
        }
        mRv_service_list.setAdapter(mServiceAdapter);
        mServiceAdapter.setData(mDetail.serviceItemList);
        if(mDetail.serviceItemList == null || mDetail.serviceItemList.size() == 0){
            mServiceDetailTitle.setVisibility(View.GONE);
        }else {
            mServiceDetailTitle.setVisibility(View.VISIBLE);
        }

        if(mShopAdapter == null){
            mShopAdapter = new ShopListAdapter(this,mDetail.id);
        }
        mRv_shop_list.setAdapter(mShopAdapter);
        mShopAdapter.setData(mDetail.shopList);
        if(mDetail.shopList == null || mDetail.shopList.size() == 0){
            mTv_goodShopTitle.setVisibility(View.GONE);
        }else {
            mTv_goodShopTitle.setVisibility(View.VISIBLE);
        }


        if(mScenicSpotAdapter == null){
            mScenicSpotAdapter = new ScenicSpotListAdapter(this,mDetail.id);
        }
        mRv_scenic_spot_list.setAdapter(mScenicSpotAdapter);
        mScenicSpotAdapter.setData(mDetail.viewSpotList);
        if(mDetail.viewSpotList == null || mDetail.viewSpotList.size() == 0){
            mTv_nearby_view_title.setVisibility(View.GONE);
        }else {
            mTv_nearby_view_title.setVisibility(View.VISIBLE);
        }

    }

}
